package com.jesterplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.renderscript.Font;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CurrentSongPanel extends SurfaceView implements
		SurfaceHolder.Callback {
	private static CanvasThread canvasthread;

	int backgroundInt = R.drawable.cubes;

	public CurrentSongPanel(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		getHolder().addCallback(this);

		canvasthread = new CanvasThread(getHolder(), this);
		setFocusable(true);
	}

	public CurrentSongPanel(Context context) {
		super(context);
		getHolder().addCallback(this);
		canvasthread = new CanvasThread(getHolder(), this);
		setFocusable(true);
	}
	
	@Override
	protected void onWindowVisibilityChanged(int visibility) {
		super.onWindowVisibilityChanged(visibility);
	};

	@Override
	public void onDraw(Canvas canvas) {
		
		Paint paint = new Paint();
		int isRusty = 1;

		try {
			drawNowPlayingScreen(canvas);
		} catch (NullPointerException npe) {

		}

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if (canvasthread.getState() == Thread.State.TERMINATED) {
			canvasthread = new CanvasThread(getHolder(), this);
			canvasthread.setRunning(true);
			canvasthread.start();
        }
        else {
        	canvasthread.setRunning(true);
        	canvasthread.start();
        }
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = true;
		canvasthread.setRunning(false);
		while (retry) {
			try {
				canvasthread.join();
				retry = false;
			} catch (InterruptedException e) {
				// we will try it again and again...
			}
		}

	}

	public void drawNowPlayingScreen(Canvas canvas) {
		Bitmap albumArt = null;
		
		
		
		Bitmap background = BitmapFactory.decodeResource(getResources(),
				backgroundInt);
		canvas.drawColor(Color.BLACK);
		
		
		
		
        int screenWidthDynamic = getWidth();
        
        double backgroundScale = ((double)(screenWidthDynamic))/background.getWidth();
        
        
        int scaledBackgroundWidth = (int)(background.getWidth()*backgroundScale);
        int scaledBackgroundHeight = (int)(background.getHeight()*backgroundScale);
        
        Bitmap scaledBackground = Bitmap.createScaledBitmap( background, scaledBackgroundWidth, scaledBackgroundHeight, true );
        
        
        
		canvas.drawBitmap(scaledBackground, 0, 0, null);	
		
		
		
		Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"fonts/Roboto-Light.ttf");

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTypeface(tf);
		paint.setTextSize(100);
		paint.setColor(Color.WHITE);

		canvas.drawText(CurrentTrack.getTitle(), 45, 190, paint);
		paint.setTextSize(90);
		canvas.drawText(CurrentTrack.getArtist(), 45, 920, paint);
		Typeface tf2 = Typeface.createFromAsset(getContext().getAssets(),"fonts/Roboto-Thin.ttf");
		paint.setTypeface(tf2);
		paint.setTextSize(80);
		canvas.drawText(CurrentTrack.getAlbum(), 45, 1015, paint);
		
		albumArt = CurrentTrack.getAlbumArt();
		double albumScale = ((double)(screenWidthDynamic-100))/albumArt.getWidth();
		int scaledAlbumWidth = (int)(albumArt.getWidth()*albumScale);
        int scaledAlbumHeight = (int)(albumArt.getHeight()*albumScale);
		if(albumArt!=null){
        	Bitmap scaledAlbumArt = Bitmap.createScaledBitmap( albumArt, scaledAlbumWidth, scaledAlbumHeight, true );
        	canvas.drawBitmap(scaledAlbumArt, 50, 220, null);
        }
	}

	public static CanvasThread getCanvasthread() {
		return canvasthread;
	}

	public static void setCanvasthread(CanvasThread canvasthread) {
		CurrentSongPanel.canvasthread = canvasthread;
	}

}